define(function (require, e, module) {

    var $ = require("zepto");

    module.exports = $;

    (function ($) {
        $.fn.wx = function (option) {
            function n(t, e) {
                var i = 2e3;
                e = e || 0;
                !0 === window.G_WEIXIN_READY || "WeixinJSBridge" in window ? t.apply(null, []) : i >= e && setTimeout(function () {
                    n(t, e++);
                }, 15);
            }
            option = $.extend({}, $.fn.wx.defaults, option);
            document.addEventListener("WeixinJSBridgeReady", function () {
                window.G_WEIXIN_READY = true;
            }, false);
            var tool = {
                execHandler: function (handler) {
                    if (handler && handler instanceof Object) {
                        var callback = handler.callback || null;
                        var args = handler.args || [];
                        var context = handler.context || null;
                        var delay = handler.delay || -1;
                        if (callback && callback instanceof Function) {
                            if ("number" === typeof delay && delay >= 0) {
                                setTimeout(function () {
                                    e.apply(context, args);
                                }, delay);
                            } else {
                                callback.apply(context, args);
                            }
                        }
                    }
                },
                execAfterMergerHandler: function (handler, e) {
                    if (handler && handler instanceof Object) {
                        var args = handler.args || [];
                        handler.args = e.concat(args);
                    }
                    this.execHandler(handler);
                }
            };
            var weixin = {
                Share: {
                    weibo: function (t, e) {
                        n(function () {
                            WeixinJSBridge.on("menu:share:weibo", function () {
                                WeixinJSBridge.invoke("shareWeibo", t, function (t) {
                                    tool.execAfterMergerHandler(e, [t]);
                                });
                            });
                        });
                    },
                    timeline: function (t, e) {
                        n(function () {
                            WeixinJSBridge.on("menu:share:timeline", function () {
                                WeixinJSBridge.invoke("shareTimeline", t, function (t) {
                                    tool.execAfterMergerHandler(e, [t])
                                });
                            });
                        });
                    },
                    message: function (t, e) {
                        n(function () {
                            WeixinJSBridge.on("menu:share:appmessage", function () {
                                WeixinJSBridge.invoke("sendAppMessage", t, function (t) {
                                    tool.execAfterMergerHandler(e, [t]);
                                });
                            });
                        });
                    }
                },
                setToolbar: function (t, e) {
                    n(function () {
                        !0 === t ? WeixinJSBridge.call("showToolbar") : WeixinJSBridge.call("hideToolbar"), tool.execAfterMergerHandler(e, [t])
                    });
                },
                setOptionMenu: function (t, e) {
                    n(function () {
                        !0 === t ? WeixinJSBridge.call("showOptionMenu") : WeixinJSBridge.call("hideOptionMenu"), tool.execAfterMergerHandler(e, [t])
                    });
                },
                pay: function (t, e) {
                    n(function () {
                        var n = {appId: "", timeStamp: "", nonceStr: "", "package": "", signType: "", paySign: ""}, i = e || {}, a = null, r = [t];
                        for (var s in n)
                            n.hasOwnProperty(s) && (n[s] = t[s] || "", console.info(s + " = " + n[s]));
                        WeixinJSBridge.invoke("getBrandWCPayRequest", n, function (t) {
                            var e = "get_brand_wcpay_request:";
                            switch (t.err_msg) {
                                case e + "ok":
                                    a = i.success;
                                    break;
                                case e + "fail":
                                    a = i.fail || i.error;
                                    break;
                                case e + "cancel":
                                    a = i.cancel || i.error;
                                    break;
                                default:
                                    a = i.error;
                            }
                            tool.execAfterMergerHandler(a, r);
                        });
                    });
                }
            };
            var weibo_option = {content: option.con};
            var option = {
                img_url: option.img,
                img_width: 180,
                img_height: 180,
                link: option.link,
                desc: option.con,
                title: option.title
            };
            return handler = {
                callback: function () {
                    window.location.href = option.callback;
                }
            };
            weixin.Share.timeline(option, handler);
            weixin.Share.message(option, handler);
            weixin.Share.weibo(weibo_option, handler);
        };
        $.fn.wx.defaults = {
            title: "云来轻APP-创新作品1号，仅限内测体验", con: "创新1号仅限内部小伙伴们尽情体验！！",
            link: document.URL,
            img: location.protocol + "//" + location.hostname + ":" + location.port + "/template/19/img/wx_img_01@2x.jpg?time=1"
        };
        $.fn.wx.version = "1.0.0";
    })($);
});